<template>
  <div class="distribute">
    <HeaderControl title="服务分配" />
    <div class="distribute-content main-content">
      <el-table :data="userList" v-loading="usersTableLoading">
        <el-table-column prop="id" label="ID" width="54px" />
        <el-table-column prop="name" label="用户名称" />
        <el-table-column prop="account" label="用户账号" />
        <el-table-column label="所属角色">
          <template #default="scope">
            <el-tag>{{ scope.row.role }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="头像">
          <template #default="scope">
            <div class="img-box">
              <div class="no-avatar" v-if="!scope.row.avatar">
                <p>{{ scope.row.name[0] }}</p>
              </div>
              <div v-else class="has-avatar">
                <img :src="scope.row.avatar" alt="">
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="管理操作" width="100px">
          <template #default="scope">
            <el-button size="small" @click="distributeServer(scope.row.id)">服务分配</el-button>
          </template>
        </el-table-column>
      </el-table>
      <PaginationView :total="queryData.total" :pageSize="queryData.pageSize" :currentPage="queryData.currentPage"
        @changeSize="handleSizeChange" @currentChange="handleCurrentChange" @refreshList="_getUserList"
        :canChoose="false" />
    </div>
    <el-dialog title="服务分配" v-model="serverDialog" width="30%">
      <el-form :model="serverForm" label-width="60px" v-loading="serverLoading">
        <el-form-item label="服务">
          <el-select v-model="serverForm.serverIds" multiple placeholder="请选择服务" style="width: 100%">
            <el-option v-for="item in serverList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span>
          <el-button @click="serverDialog = false">取消</el-button>
          <el-button type="primary" @click="serverConfirm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
/* eslint-disable */
import { ref, reactive } from 'vue'
import { getUserList } from '@/api/user'
import { setUserServer, getUserServer, getServerList } from '@/api/server'
import { ElMessage } from 'element-plus'

// 获取所有服务
const serverList = ref([])
const _getServerList = async () => {
  const res = await getServerList()
  if (res.code === 20000) {
    serverList.value = res.data.list
  }
}
_getServerList()
// 分页获取用户
const defaultQuery = reactive({
  total: 0,
  currentPage: 1,
  pageSize: 15,
})
const queryData = ref(Object.assign({}, defaultQuery))
const usersTableLoading = ref(false)
const userList = ref([])
const _getUserList = async () => {
  usersTableLoading.value = true
  try{
    let res = await getUserList(queryData.value)
    userList.value = [...res.data.list]
    queryData.value.total = res.data.total
  }catch{
    userList.value = []
  }
  usersTableLoading.value = false
}
_getUserList()
// 分配服务
const serverLoading = ref(false)
const serverDialog = ref(false)
const serverForm = ref({
  userId: '',
  serverIds: []
})
const distributeServer = async (id) => {
  serverDialog.value = true
  serverLoading.value = true
  serverForm.value.userId = id
  let  res = await getUserServer({ userId: id })
  if (res.code === 20000) {
    serverForm.value.serverIds = res.data.map(item => item.id)
  }
  serverLoading.value = false
}
const serverConfirm = () => {
  setUserServer(serverForm.value).then(res => {
    if(res.code === 20000){
      ElMessage.success('分配成功')
      serverDialog.value = false
    }
  })
}
/*****************************分页器start*********************************/
// 页数修改
const handleCurrentChange = (page) => {
  queryData.value.currentPage = page
  _getUserList()
}
const handleSizeChange = (size) => {
  queryData.value.pageSize = size
  _getUserList()
}
/*****************************分页器end**********************************/
</script>

<style lang='scss' scoped>
.distribute {
  .distribute-content {
    margin-top: 20px;

    .img-box {
      height: 50px;
      width: 50px;
      border-radius: 50%;
      overflow: hidden;
      position: relative;

      .no-avatar {
        display: flex;
        background-color: $mainColor;
        height: 100%;
        width: 100%;
        justify-content: center;
        align-items: center;

        p {
          color: #fff;
          font-size: 32px;
        }
      }

      .has-avatar {
        height: 100%;
        width: 100%;

        img {
          height: 100%;
          width: 100%;
        }
      }
    }
  }
}
</style>